﻿using A.M.ETO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using Public_Class_Library;
using System.Collections.Generic;
using W.M.Materiel.Application.DTO;
using W.M.Materiel.Application.IServices;
using W.M.Materiel.Domain.Entity;
using W.M.Materiel.Infrastruecure.DTO;


namespace W.M.Materiel.Read.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Product_management_tableController : ControllerBase
    {
        private readonly IProduct_management_tableServices product_Management_TableServices;

        private readonly Cross_service_Helper cross_service_Helper;

        public Product_management_tableController(IProduct_management_tableServices product_Management_TableServices, Cross_service_Helper cross_service_Helper)
        {
            this.product_Management_TableServices = product_Management_TableServices;
            this.cross_service_Helper=cross_service_Helper;
        }


        /// <summary>
        /// 跨服务访问接口-必须是HttpGet协议，否则报405
        /// </summary>
        /// <param name="URL"></param>
        /// <returns></returns>
        [HttpGet]
        public  async Task<IActionResult> Getunit(string? URL)
        {
            var list = await cross_service_Helper.Get(URL);
            return Ok(list);
        }

        /// <summary>
        /// 产品信息+分页+查询
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="name">查询条件:产品名称</param>
        /// <param name="id">查询条件:单位id</param>
        /// <returns>产品信息</returns>
        [HttpGet]
        public IActionResult GetProductionunit(int pageSize, int pageIndex, string? name, string? id)
        {
            int totalCount;
            int pageCount;
            var list= product_Management_TableServices.GetProductionunit(pageSize, pageIndex, name, id, out totalCount, out pageCount);
            return Ok(new
            {
                totalCount,pageCount,list
            });
        }

        /// <summary>
        /// 添加产品信息
        /// </summary>
        /// <param name="c">产品实体</param>
        /// <returns>统一返回格式</returns>
        [HttpPost]
        public async Task<IActionResult> GetproductAdd(product_management_table c)
        {
            var list =await product_Management_TableServices.GetproductAdd(c);
            return Ok(list);
        }

        /// <summary>
        /// 删除产品信息
        /// </summary>
        /// <param name="id">产品编号</param>
        /// <returns>统一返回格式</returns>
        [HttpPost]
        public async Task<IActionResult> GetproductDel(string? id)
        {
            var list =await product_Management_TableServices.GetproductDel(id);
            return Ok(list);
        }

        /// <summary>
        /// 修改产品信息
        /// </summary>
        /// <param name="c">产品实体</param>
        /// <returns>统一返回格式</returns>
        [HttpPost]
        public async Task<IActionResult> GetproductUpdate(product_management_table c)
        {
            var list =await product_Management_TableServices.GetproductUpdate(c);
            return Ok(list);
        }

        /// <summary>
        /// 反填产品信息
        /// </summary>
        /// <param name="product_number">产品编号</param>
        /// <returns>统一返回格式</returns>
        [HttpGet]
        public async Task<IActionResult> GetBillbackfill(string? product_number)
        {
            var list=await product_Management_TableServices.GetBillbackfill(product_number);
            return Ok(list);
        }

        /// <summary>
        /// 反填产品信息
        /// </summary>
        /// <param name="id">产品编号</param>
        /// <returns>产品数据</returns>
        [HttpGet]
        public async Task<product_management_table> GetvackfillProduct(string? id)
        {
            var obj = await product_Management_TableServices.GetShowObjAsync(c => c.product_number == id);
            return obj;
        }

        /// <summary>
        /// 给钱球类的不要动
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetQshowinfor()
        {
            return Ok(await product_Management_TableServices.GetProductInfor());
        }

        /// <summary>
        /// 产品导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ExportTiproductExcel()
        {
            var list = product_Management_TableServices.GetShowInfor();
            var steram = new MemoryStream();
            steram.SaveAs(list);
            steram.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(steram, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "产品信息.xlsx"
            };
        }
    }
}
